package com.general.app.wjgl.action;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.general.app.wjgl.dao.entity.Factor;
import com.general.app.wjgl.service.IFactorService;
import com.general.common.action.BaseAction;
import com.general.common.model.QueryModel;
import com.general.utils.base.Constant;
import com.general.utils.base.UuidUtil;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
/**
 * 问卷因子管理
 * @author Administrator
 *
 */
public class FactorAction extends BaseAction implements ModelDriven<Factor>{

	private static final long serialVersionUID = -3736194854316309204L;
	
	private Factor model = new Factor();
	private IFactorService factorService ;

	@Override
	public Factor getModel() {
		return model;
	}

	public void setFactorService(IFactorService factorService) {
		this.factorService = factorService;
	}
	
	
	/**
	 * 问卷因子查询
	 * @return
	 */
	public String getFactorList(){
		
		QueryModel queryModel = model.getQueryModel();
		List<Factor> factorList = factorService.getPagedList(model);
		queryModel.setItems(factorList);
		ValueStack vs = getValueStack();
		vs.set(DATA, queryModel);
		
		return DATA;
	}
	
	public String factorList(){
		
		return "factorList";
	}
	
	public String toAddFactor(){
		
		return "addFactor";
	}

	
	/**
	 * 保存问卷因子
	 * @return
	 */
	public String addFactor(){
		model.setId(UuidUtil.getInstance().getUniqID());
		boolean result = factorService.insert(model);
		String key = result ? Constant.SAVE_SUCCEE : Constant.SAVE_FAIL;
		getValueStack().set(DATA, getText(key));
		return DATA;
	}
	
	
	/**
	 * 修改问卷因子
	 * @return
	 */
	public String toUpadteFactor(){
		String id = model.getId();
		Factor factor = factorService.getModel(id);
		
		try {
			BeanUtils.copyProperties(model, factor);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		} 
		return "upadteFactor";
	}
	
	
	/**
	 * 修改问卷因子
	 * @return
	 */
	public String updateFactor(){
		boolean result = factorService.update(model);
		String key = result ? Constant.SAVE_SUCCEE : Constant.SAVE_FAIL;
		getValueStack().set(DATA, getText(key));
		return DATA;
	}
	
	
	/**
	 * 删除问卷因子
	 * @return
	 */
	public String deleteFactor(){
		String ids = getRequest().getParameter("ids");
		try {
			
			int delCount = factorService.delete(ids);
			getValueStack().set(DATA, getText(Constant.DELETE_INFO,new String[]{"问卷因子",String.valueOf(delCount)}));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}
}
